package com.sx.sx1.tools;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class SFTPReader {

    public static void main(String[] args) {
        readerRemoteDirAndFile("/data/ruoyi/upload/1319",1319);
    }

    //remoteDir:  "/data/ruoyi/upload/1319"    问题数据 1319 是任务ID
    //jobid: 任务ID
    public static void readerRemoteDirAndFile( String remoteDir,Integer jobid) {
        String host = "192.168.101.128";
        int port = 22;
        String user = "root";
        String password = "123456";
        //String remoteDir = "/data/ruoyi/upload/1319";

        JSch jsch = new JSch();
        Session session = null;
        Channel channel = null;
        ChannelSftp sftpChannel = null;

        try {
            // Setup JSch session.
            session = jsch.getSession(user, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

            // Open SFTP channel.
            channel = session.openChannel("sftp");
            channel.connect();
            sftpChannel = (ChannelSftp) channel;

            // Change to the remote directory.
            sftpChannel.cd(remoteDir);

            // List the files in the remote directory.
            Vector<ChannelSftp.LsEntry> list = sftpChannel.ls("*");
            List<String>  emptyFiles = new ArrayList<>();
            List<String>  notemptyFiles = new ArrayList<>();
            for (ChannelSftp.LsEntry entry : list) {
                String filename = entry.getFilename();


                // Read the file content.
                InputStream is = sftpChannel.get(filename);
                // Do something with the input stream, for example, print it.
                // Or, save it to a local file.
                // is.close();


                StringBuilder builder = new StringBuilder();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line).append('\n');
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (builder.length() > 0 && builder.charAt(builder.length() - 1) == '\n') {
                    // 判断去掉最后一个换行符
                    builder.setLength(builder.length() - 1);
                }
                //System.out.println(builder);

                if(builder.length() ==0) {
                    emptyFiles.add(filename);
                }else{
                    notemptyFiles.add(filename);
                }
            }

            //System.out.println(notemptyFiles);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sftpChannel != null) {
                sftpChannel.exit();
            }
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        }
    }
}
